package com.jbp.utils;

import java.util.Random;
import java.util.regex.Pattern;

public class CreateUUid {

    /**
     *匹配6位顺增
     */
    private static String one="(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){5}\\d";

    /**
     * 匹配6位顺降
     */
    private static  String tow="(?:9(?=8)|8(?=7)|7(?=6)|6(?=5)|5(?=4)|4(?=3)|3(?=2)|2(?=1)|1(?=0)){5}\\d";

    /**
     * 匹配3位以上的重复数字
     */
    private static String three="([\\d])\\1{2,}";

    /**
     * 匹配33111类型的
     */
    private static String four="([\\d])\\1{1,}([\\d])\\2{2,}";

    /**
     * 匹配5331533类型的
     */
    private static String five="(([\\d]){1,}([\\d]){1,})\\1{1,}";

    /**
     * 匹配22334,123355类型的
     */
    private static String six="([\\d])\\1{1,}([\\d])\\2{1,}";


    /**
     * 用户uuId
     * @return
     */
    public static String getUuid(){
        Random rand=new Random();//生成随机数
        String cardNnumer="";
        for(int a=0;a<8;a++){
            cardNnumer+=rand.nextInt(10);//生成6位数字
        }
        return cardNnumer;
    }

    /**
     * 个人房
     * @return
     */
    public static String getRoomUuid(){
        Random rand=new Random();//生成随机数
        String cardNnumer="";
        for(int a=0;a<7;a++){
            cardNnumer+=rand.nextInt(10);//生成6位数字
        }
        return cardNnumer;
    }

    public static String createRoomCode(){
        String code=getRoomUuid();
        if (code.startsWith("0")) {
            return createRoomCode();
        }
        boolean isMatch = Pattern.matches(code, one);
        if (isMatch){
            return createRoomCode();
        }
        isMatch = Pattern.matches(code, tow);
        if (isMatch){
            return createRoomCode();
        }
        isMatch = Pattern.matches(code, three);
        if (isMatch){
            return createRoomCode();
        }
        isMatch = Pattern.matches(code, four);
        if (isMatch){
            return createRoomCode();
        }
        isMatch = Pattern.matches(code, five);
        if (isMatch){
            return createRoomCode();
        }
        isMatch = Pattern.matches(code, six);
        if (isMatch){
            return createRoomCode();
        }

        return  code;
    }

    public static String createCode(){
        String code=getUuid();
        if (code.startsWith("0")) {
            return createCode();
        }
        boolean isMatch = Pattern.matches(code, one);
        if (isMatch){
            return createCode();
        }
         isMatch = Pattern.matches(code, tow);
        if (isMatch){
            return createCode();
        }
         isMatch = Pattern.matches(code, three);
        if (isMatch){
            return createCode();
        }
         isMatch = Pattern.matches(code, four);
        if (isMatch){
            return createCode();
        }
         isMatch = Pattern.matches(code, five);
        if (isMatch){
            return createCode();
        }
         isMatch = Pattern.matches(code, six);
        if (isMatch){
            return createCode();
        }

        return  code;
    }

    public static void main(String args[]){
        for (int i = 0; i <1000 ; i++) {
            System.out.println(createCode());
        }
    }

}

